cmake_minimum_required(VERSION 3.10)

# 项目名称和版本
project(supervisor VERSION 1.0 LANGUAGES C CXX)


# 设置 C++ 标准
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_C_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED True)

set(OS_ARCH "arm")

if(CMAKE_SYSTEM_NAME MATCHES "Linux")
    if(OS_ARCH MATCHES "arm")
        set(CMAKE_SYSTEM_NAME Linux)
        set(CMAKE_SYSTEM_PROCESSOR arm)
        MESSAGE(STATUS "current build compiler: linux ${OS_ARCH}")
        # TODO 需要修改对应的路径
        set(CMAKE_SYSTEM_NAME Linux)
        set(CMAKE_SYSTEM_PROCESSOR arm)
        set(CMAKE_C_COMPILER aarch64-xilinx-linux-gcc)
        set(CMAKE_CXX_COMPILER aarch64-xilinx-linux-g++)
        set(ROOT_PATH $ENV{SDKTARGETSYSROOT})

        set(CMAKE_FIND_ROOT_PATH ${ROOT_PATH})
        set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
        set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
    endif()
endif()



# 添加源文件
file(GLOB SOURCE_FILES
    ${CMAKE_CURRENT_SOURCE_DIR}/*.cc
    ${CMAKE_CURRENT_SOURCE_DIR}/*.c
)

# 添加头文件目录
include_directories(
    ${CMAKE_CURRENT_SOURCE_DIR}
)

# 创建可执行文件
add_executable(${PROJECT_NAME}  ${SOURCE_FILES} ${CMAKE_SOURCE_DIR}/res.o)
target_link_libraries(${PROJECT_NAME}  pthread)

add_custom_command(
    OUTPUT ${CMAKE_SOURCE_DIR}/res.o
    COMMAND ${CMAKE_COMMAND} -E echo "Packing res.zip and converting to res.o"
    COMMAND zip -r res.zip res
    COMMAND aarch64-xilinx-linux-objcopy -I binary -B aarch64 -O elf64-littleaarch64
            res.zip res.o
    DEPENDS ${CMAKE_SOURCE_DIR}/res  # 如果 res 目录内容变化，重新构建
    WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
    COMMENT "Generating res.o from res/"
)
add_custom_target(generate_res_o DEPENDS ${CMAKE_SOURCE_DIR}/res.o)
add_dependencies(${PROJECT_NAME} generate_res_o)

add_custom_command(
    TARGET ${PROJECT_NAME} POST_BUILD
    COMMAND ${CMAKE_COMMAND} -E echo "afterbuild_command"
    COMMAND rm -f res.o res.zip
    WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
)
